import { Component, OnInit } from '@angular/core';
import { getCity, getList } from '../api/index.js'
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  tableIndex = 0;
  loading1 = false;
  total1 = 0;
  total2 = 0;
  input1 = '';
  selectedValue = '';
  date = [];
  loading2 = false;
  table1 = [];
  table2 = [];
  cityData = [];
  curTime = {};
  sendData1 = {
    type: 0,
    centerId: '',
    time: '',
    page_num: 1
  };
  sendData2 = {
    type: 1,
    startTime: '',
    endTime: '',
    title: '',
    page_size: 5,
    page_num: 1
  };
  disabledDate = (current: Date) => {
    return this.getTime(current)
  };
  ngOnInit() {
    console.log(2)
    this.getTime()
    this.getFnCity()
    this.getFnData(0)
  }
  getTime(current?) {
    const date = new Date();
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const day = date.getDate();
    const curTime = {
      year,
      month,
      day,
      time: year + '-' + month + '-' + day
    }
    this.curTime = curTime;
    this.sendData1.time = curTime.time;
    this.sendData2.startTime = curTime.time;
    this.sendData2.endTime = curTime.time;
    if (current) {
      console.log(1)
      return new Date(current).getTime() <=  new Date(curTime.time).getTime();
    }
  }
  getFnCity() {
    getCity().then(res => {
      this.cityData = res.data;
    }).catch(err => {
      console.log('err: ', err);
    })
  }
  getFnData(num) {
    const sendData = num === 0 ? this.sendData1 : this.sendData2;
    const that = this;
    // that.loading1 = num === 0 ? true : false;
    // that.loading2 = num === 0 ? false : true;
    getList(sendData).then(res => {
      console.log('res: ', res);
      setTimeout(() => {
        if (num === 0) {
          const arr = {};
          const newArr = [];
          res.data.forEach(ids => {
            if (!arr[ids.centerId]) {
              arr[ids.centerId] = [];
            }
            arr[ids.centerId].push(ids);
          })
          console.log('arr: ', arr);
          Object.keys(arr).forEach(ids => {
            const iis = { name: '', data: [], monu: '', num: 0, time: '' };
            this.cityData.forEach(item => {
              if (ids === item.cname) {
                if (!iis.name) {
                  iis.name = item.name
                }
                iis.monu = arr[ids].map(dds => dds.month).sort((a, b) => a - b).join(',');
                const cTime = arr[ids].sort((a, b) => {
                  return new Date(b.date).getTime() - new Date(a.date).getTime()
                });
                iis.time = cTime[0].date;
                iis.num = arr[ids].reduce((prev, cur) => {
                  const prevNum = prev && prev.num || 0;
                  const curNum = cur && cur.num || 0;
                  return prevNum + curNum
                })
                // iis.data.push(arr[ids])
                iis.data = arr[ids]
              }
            })
            newArr.push(iis)
          })
          that.table1 = newArr;
          that.loading1 = false;
        } else {
          that.table2 = res.data;
          that.loading2 = false;
          that.total2 = res.total;
        }
      }, 0)
    }).catch(err => {
      if (num === 0) {
        this.loading1 = false;
      } else {
        this.loading2 = false;
      }
      // console.log('err: ', err);
    })
  }
  checkTab(num) {
    this.tableIndex = num;
    this.getFnData(num)
  }
  onChangeTime() {
  }

}
